{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true,
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "import  numpy as np\n",
    "from keras.optimizers import SGD,Adam\n",
    "from keras.layers.core import  Dense, Dropout, Activation\n",
    "from keras.layers import  Conv2D, MaxPooling2D, Flatten,PReLU\n",
    "from keras.models import Sequential, Model"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "# 用来生成网络模型\n",
    "# parameter: 输入图像的尺寸\n",
    "def get_model(shape):\n",
    "    # keras中的Sequential是顺序模型，用于构建神经网络\n",
    "    model = Sequential()\n",
    "    # 构建网络，本文根据一篇自动驾驶论文来构建网络。\n",
    "    # Conv是卷积层 代表添加一个卷积层\n",
    "        # @parameter\n",
    "            # Par1:输出通道的数量  Par2: 卷积核的尺寸 Par3: 步长 Par4: 填充（卷积核是否识别两侧边界） Par5: 激活层 Par6: 输入图像尺寸\n",
    "    model.add(Conv2D(24,(5,5),strides=(2,2),padding='valid',activation='relu',input_shape=shape))\n",
    "    model.add(Conv2D(36,(5,5),strides=(2,2),padding='valid',activation='relu'))\n",
    "    model.add(Conv2D(48,(5,5),strides=(2,2),padding='valid',activation='relu'))\n",
    "    model.add(Conv2D(64,(3,3),strides=(1,1),padding='valid',activation='relu'))\n",
    "    model.add(Conv2D(64,(3,3),strides=(1,1),padding='valid',activation='relu'))\n",
    "\n",
    "    # 权连接层和卷积层之间需要有一个连接层\n",
    "    model.add(Flatten())\n",
    "\n",
    "    # 最后是一部分权连接层\n",
    "    model.add(Dense(1164,activation='relu'))\n",
    "    model.add(Dense(100,activation='relu'))\n",
    "    model.add(Dense(50,activation='relu'))\n",
    "    model.add(Dense(10,activation='relu'))\n",
    "    # 因为是一个回归问题 结果会返回一个数值，负值代表向左打方向盘的角度 正值则是向右，如果是 relu 作为激活函数，输出值只能是 [0, ∞] 使用 linear 则输出值是全体实数\n",
    "    model.add(Dense(1,activation='linear'))\n",
    "\n",
    "    # 定义好网络后进行编译 需要设置优化器和损失函数\n",
    "    model.compile(optimizer=Adam(lr=0.01),loss='mean_squared_error')\n",
    "    return model"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}